<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>统计包含的字符串</title>
	</head>
	<body>
		<script type="text/javascript" charset="UTF-8">
			var list = new Array("America", "Greece","Britain","Canada","China","Egypt");
			// var list = ["America", "Greece","Britain","Canada","China","Egypt"];
			var str = list.join(",");//数组放到字符串里
			var s = 0;
			var S = 0;
			/*方式1
			 * for (var i = 0; i < str.length; i++) {
				if((str.charAt(i))=="a") {
					s++;
				}else if((str.charAt(i))=="A"){
					S++;
				}
			}*/
			/*//方式2
			 * for (var i = 0; i < str.length; i++) {
				if(i < str.length-1){
					var ns = str.substring(i,(i+1))
					if(ns == "a"){
						s++;
					} else if( ns == "A"){
						S++;
					}
				}
				
				if( i == (str.length-1)) {
					if((str.charAt(i))=="a") {
					s++;
					}else if((str.charAt(i))=="A"){
					S++;
					}
				}
			}*/
			for (var i = str.length-1; i >= 0 ; i--) {
				
				if((str.indexOf("a",i)) != -1) {
					s++;
				}else if ((str.indexOf("A",i)) != -1){
					S++;
				}
				str = str.substring(0,i);
			}
			document.write("a:" + s + "<br/>");
			document.write("A:"+ S);
		</script>
	</body>
</html>
